# class Engine:
#     def __init__(self, horsepower):
#         self.horsepower = horsepower
#
#     def start(self):
#         print("Engine is starting...")
#
#
#
# class Car:
#     def __init__(self, make, model, engine_horsepower):
#         self.make = make
#         self.model = model
#         # Car类创建Engine类的实例，并作为自己的成员
#         self.engine = Engine(engine_horsepower)
#
#     def drive(self):
#         self.engine.start()
#         print(f"Driving {self.make} {self.model} with {self.engine.horsepower} HP engine.")
#
#
#
# # 示例使用
# my_car = Car("Toyota", "Camry", 200)
# my_car.drive()

# 不能直接访问或操作Engine的实例，因为它依赖于Car的存在  
# 下面的代码会导致错误，因为我们没有直接创建Engine的实例的权限或需要  
# engine_instance = Engine(200)  # 错误的使用方式，因为Engine的实例应该由Car来管理


# class Department:
#     def __init__(self, name):
#         self.name = name
#
# class Company:
#     def __init__(self, name):
#         self.name = name
#         self.departments = []
#
#     def add_department(self, department):
#         self.departments.append(department)
#
#     def describe(self):
#         print(f"Company {self.name} has the following departments:")
#         for department in self.departments:
#             print(f" - {department.name}")
#
# company = Company("TechCorp")
# company.add_department(Department("Engineering"))
# company.add_department(Department("Marketing"))
# company.describe()

class Organ:
    def __init__(self, name, person):
        self.name = name  # 器官名称
        self.person = person  # 绑定到的人（内部使用，外部不直接访问）

    def __repr__(self):
        return f"Organ(name={self.name})"

class Person:
    def __init__(self, name):
        self.name = name  # 人的名称
        # 在创建人时同时创建其器官，并将它们绑定到当前人
        self.heart = Organ("Heart", self)
        self.lung = Organ("Lung", self)
        self.brain = Organ("Brain", self)
        self.eyes = Organ("eyes", self)

    def __repr__(self):
        # 使用列表推导式和join方法来生成包含所有器官表示的字符串
        organs_repr = "\n".join(f"  {repr(organ)}" for organ in [self.heart, self.lung, self.brain, self.eyes])
        return f"Person(\n name={self.name} \n organs=[\n{organs_repr}\n ]\n)"

if __name__ == "__main__":
    # 创建一个人，同时自动创建其器官
    person = Person("Lili")

    # 打印人及其器官
    print(person)